package com.app.base.app;

import android.app.ProgressDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.caszx.chargingpile.R;
import http.okhttp.OkHttpUtils;


/**
 * 含有网络请求和Topbar的Activity，刚进入时先加载布局，可能会含有网络交互的的按钮<p>
 * setContentView(int layoutResID)装载布局
 * */
public  abstract class BaseTopBarDelayActivity extends BaseActivity{
	protected ProgressDialog progDialog = null;
	protected Toolbar  mToolbar;

	@Override
	protected void init() {
		
	}

	/**
	 * 隐藏进度框
	 */
	protected void dissmissProgressDialog() {
		if (progDialog != null) {
			progDialog.dismiss();
		}
	}
	
	/**
	 * 显示进度框
	 */
	protected void showProgressDialog(String msg) {
		if (progDialog == null)
			progDialog = new ProgressDialog(this);
		progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progDialog.setIndeterminate(false);
		progDialog.setCancelable(false);
		progDialog.setMessage(msg);
		progDialog.show();
	}
	@Override
	public void setContentView(int layoutResID) {
		 super.setContentView(layoutResID);
		 initToolSuperBar();
		 findView();
	}
	
	
	/**
	 * 设置标题栏的中间的标题，但是要有ToolBar
	 * */
	protected void setCenterTitle(String title){
		TextView centerTitle = (TextView) findViewById(R.id.center_title);
		centerTitle.setText(title);
	}
	
	/**
	 * 设置标题栏的中间的标题，但是要有ToolBar
	 * */
	protected void setCenterTitle(int resId){
		TextView centerTitle = (TextView) findViewById(R.id.center_title);
		centerTitle.setText(resId);
	}
	
	/**
	 * initView()初始化根布局，返回的View 作为setContentView的参数
	 * 
	 * findView()执行findViewById()之类的工作
	 * 
	 */
	protected  abstract void  findView() ;
	
	protected void initToolSuperBar( ) {
		mToolbar = (Toolbar) findViewById(R.id.toolbar);
		if(mToolbar!=null){
			mToolbar.setTitleTextColor(getResources().getColor(R.color.white));
			mToolbar.setTitle("");
			setSupportActionBar(mToolbar);
			mToolbar.setBackgroundColor(getResources().getColor(R.color.toolbarColor));
			mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onBackPressed();
				}
			});
			getSupportActionBar().setHomeButtonEnabled(true);
			getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
		
		
	}
	/**
	 * 取消tag标签的请求
	 */
	protected void cancelAll(Object tag){
		OkHttpUtils.getInstance().cancelTag(tag);
		//RequestHttp.getRequestQueue().cancelAll(tag);
	}
	
	@Override
	protected void onDestroy() {
		cancelAll(this);
		super.onDestroy();
	}
	
}
